<template>
  <view id="qualityReport">
		<view class="nav-box">
			<view class="nav-box-text">
				<image src="@/static/img/home/person.png" class="person"></image>
				<view class="welcome">
					<view><text>姓名：{{ userInfo.nickName }}</text></view>
					<view><text>单位：{{ userInfo.dept.deptName }}</text></view>
				</view>
			</view>
		</view>
		<view class="project">
			<text>{{ projectInfo.name }}</text>
			<button type="primary" size="mini" @click="popupToggle('open')"><uni-icons type="search" color="#fff" size="14" /></button>
		</view>
		<!-- 检测信息 -->
		<view class="uni-list-box" v-if="listData.length > 0">
			<view class="list-item" v-for="(item, index) in listData" :key="index">
				<view class="list-item-line title">
					<text class="item-text ">工程名称:{{ item.projectName }}</text>
				</view>
				<view class="list-item-line">
					<text class="sub-text">检测机构</text>
					<text class="sub-text u-margin-left-40 hide">{{ item.checkCompanyName }}</text>
				</view>
				<view class="list-item-line">
					<text class="sub-text" >报告编号</text>
					<text class="sub-text u-margin-left-40">{{ item.reportCode }}</text>
				</view>
				<view style="display: flex;justify-content: flex-end;">
					<uni-tag text="报告查看" class="u-margin-top-15" type="primary" size="small" @click="toPath(item, 1)" />
					<uni-tag text="报告授权" class="u-margin-top-15 u-margin-left-10" type="primary" size="small" @click="authorize(item)" v-if="userInfo.dept.type == 'build'" />
					<uni-tag text="报告核验" class="u-margin-top-15 u-margin-left-10" type="primary" size="small" @click="toPath(item, 2)" v-if="userInfo.dept.type == 'supervise' && !item.hyResult" />
					<uni-tag text="报告归档" class="u-margin-top-15 u-margin-left-10" type="primary" size="small" @click="archive(item)" v-if="userInfo.dept.type == 'construct' && !item.doneTime" />
				</view>
			</view>
		</view>
		<uni-load-more :status="status" :icon-size="16" :content-text="contentText" v-if="listData.length > 0" />
		<view style="text-align: center;margin: 20px;" v-if="listData.length == 0"><text>暂无数据</text></view>
		<!-- 搜索内容 -->
		<uni-popup ref="sampleSearchPopup" :maskClick="false" background-color="#fff">
			<scroll-view style="height: calc(100vh - 44px);" scroll-y="true">
				<view class="popup-search">
					<uni-section title="条件搜索" type="line" style="margin-top: 0;"></uni-section>
					<uni-forms :modelValue="searchForm" class="search-box" :label-width="85" border>
						<uni-forms-item label="报告编码" name="reportCode">
							<uni-easyinput type="text" v-model="searchForm.reportCode" placeholder="请输入报告编码" />
						</uni-forms-item>
						<uni-forms-item label="日期" name="reportTime">
							<uni-datetime-picker v-model="searchForm.reportTime" type="date" />
						</uni-forms-item>
						<!-- <uni-forms-item label="二维码编码" name="name">
							<uni-easyinput type="text" v-model="searchForm.code" placeholder="请输入二维码编码" />
						</uni-forms-item>
						<uni-forms-item label="工程名称" name="name">
							<uni-easyinput type="text" v-model="searchForm.projectName" placeholder="请输入工程名称" />
						</uni-forms-item>
						<uni-forms-item label="检测项目" name="name">
							<uni-easyinput type="text" v-model="searchForm.checkItem" placeholder="请输入检测项目" />
						</uni-forms-item>
						<uni-forms-item label="取样员" name="name">
							<uni-easyinput type="text" v-model="searchForm.checker" placeholder="请输入取样员" />
						</uni-forms-item>
						<uni-forms-item label="取样时间" name="name">
							<uni-datetime-picker v-model="searchForm.sampleTime" :border="false" type="daterange"  />
						</uni-forms-item>
						<uni-forms-item label="见证人" name="name">
							<uni-easyinput type="text" v-model="searchForm.jzr" placeholder="请输入见证人" />
						</uni-forms-item>
						<uni-forms-item label="检测进度" name="name">
							<picker @change="checkProgressChange" :value="checkProgressIndex" :range="checkProgressOptions" range-key="name">
								<view class="uni-input">{{checkProgressOptions[checkProgressIndex].name}}</view>
							</picker>
						</uni-forms-item> -->
					</uni-forms>
					<button @click="search" type="primary" class="u-margin-20">查询</button>
					<button @click="popupToggle('close')" type="default" class="u-margin-20" style="background-color: #fff;">取消</button>
				</view>
			</scroll-view>
		</uni-popup>
		<!-- 报告授权 -->
		<uni-popup ref="reportAuthorizePopup" background-color="#fff" :mask-click="false"  type="dialog">
			<uni-popup-dialog  title="报告授权" type="input" :duration="2000" @confirm="dialogConfirm">
				<uni-data-checkbox multiple v-model="authorizeUnit" :localdata="authorizeUnitOptions"></uni-data-checkbox>
			</uni-popup-dialog >
		</uni-popup>
		
		<!-- 报告归档 -->
		<uni-popup ref="reportArchivePopup" background-color="#fff" :mask-click="false"  type="dialog">
			<uni-popup-dialog  title="报告归档" type="info" content="是否已经核验报告内容确认无误" :duration="2000" @confirm="complateConfirm"></uni-popup-dialog >
		</uni-popup>
  </view>
</template>

<script>
import { reportList, reportAuth, reportDone } from '@/api/qualityReport/index'
import { decrypt } from '@/common/jsencrpty.js'
	
export default {
  data() {
    return {
			// 列表数据
			listData: [],
			// 查询参数
			searchForm:{
				reportCode:'',
				reportTime:'',
				// code: "",
				// projectName: "",
				// checkItem: "",
				// checker: "",
				// jzr: "",
				// sampleTime: [],
				// checkProgress: null,
				pageNum: 1,
				pageSize: 10,
				total: 0
			},
			contentText: {
				contentdown: '上拉加载更多',
				contentrefresh: '加载中',
				contentnomore: '没有更多数据了'
			},
			// 检测进度
			checkProgressOptions: [
				{ value: 1, name: '已委托' }, 
				{ value: 2, name: '检测中' }
			],
			checkProgressIndex:0,
			// 授权的单位
			authorizeUnit:[],
			authorizeUnitOptions:[
				{ value:"1", text:"施工单位" },
				{ value:"2", text:"监理单位" }
			],
			reportId: ''
    }
  },
  onLoad() {
    this.userInfo = uni.getStorageSync('userInfo')
	this.userInfo.nickName = decrypt(this.userInfo.nickName)
    this.projectInfo = uni.getStorageSync('projectInfo')
		this.getList()
  },
  onReachBottom() {
    if (this.status != 'noMore') {
    	this.reload = true
    	this.status = 'more'
    	this.searchForm.pageNum++
    	this.getList()
    }
  },
  methods: {
	  //搜索
	  search(){
		  reportList(this.searchForm).then(res=>{
		  	this.listData = res.rows
		  	this.searchForm.total = res.total
		  })
		this.popupToggle('close')  
	  },
		// 确认授权
		dialogConfirm() {
			let id = this.reportId, type = 0
			if (this.authorizeUnit.length == 0) {
				type = 0
			} else if (this.authorizeUnit.length == 2) {
				type = 3
			} else {
				if (this.authorizeUnit[0] == 1) {
					type = 1
				} else {
					type = 2
				}
			}
			reportAuth(id, type).then(res=>{
				if (res.code == 200) {
					uni.showToast({
						title: '授权成功'
					})
					this.listData = []
					this.getList()
				}
			})
		},
		// 弹出层开关
		popupToggle(flag) {
			if (flag == "open") {
				this.$refs.sampleSearchPopup.open('right')
			} else {
				this.$refs.sampleSearchPopup.close()
			}
		},
		// 查询检测进度改变
		checkProgressChange() {
			
		},
		// 信息查看
		toPath(item, type) {
			let path = ''
			if (type ==1) {
				path = '/pages/qualityReport/CheckReport?path='+item.reportFile.url
			} else {
				path = '/pages/qualityReport/VerificationReport?id='+item.id+'&path='+item.reportFile.url
			}
			uni.navigateTo({
				url: path
			})
		},
		// 报告授权
		authorize(item) {
			this.authorizeUnit = []
			if (item.sgdwView) {
				this.authorizeUnit.push('1')
			}
			if (item.jldwView) {
				this.authorizeUnit.push('2')
			}
			this.reportId = item.id
			this.$refs.reportAuthorizePopup.open('center')
		},
		// 确认弹窗
		archive(item) {
			this.reportId = item.id
			this.$refs.reportArchivePopup.open('center')
		},
		// 报告归档
		complateConfirm() {
			uni.showLoading({
				mask: true,
				title: '报告归档中'
			})
			reportDone(this.reportId).then(res=>{
				uni.hideLoading()
				if (res.code == 200) {
					uni.showToast({
						title: '报告归档成功'
					})
					this.listData = []
					this.getList()
				}
			}).catch(error=>{
				uni.hideLoading()
			})
		},
		// 列表查询
		getList() {
			this.status = 'loading'
			reportList(this.searchForm).then(res=>{
				this.reload = false
				this.listData = this.listData.concat(res.rows)
				this.searchForm.total = res.total
				if (this.searchForm.pageNum*this.searchForm.pageSize >= res.total) {
					this.status = 'noMore'
				} else {
					this.status = 'more'
				}
			})
		}
  }
}
</script>
<style lang="scss" scoped>
	#qualityReport {
		min-height: 100vh;
		background-color: #fafafa;
		
		.nav-box {
			width: 100vw;
			height: 160px;
			margin-top: -44px;
			padding: 56px 20px 56px 20px;
			background-image: url(../../static/img/home/head-bg.png);
			
			.nav-box-text {
				height: 50px;
				display: flex;
				align-items: center;
				
				.person {
					width: 48px;
					height: 48px;
				}
				.welcome {
					color: #333;
					font-size: 32rpx;
					margin-left: 40rpx;
				}
			}
		}
		
		.project {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 10px;
			border-radius: 8px;
			background-color: #fff;
			margin: -32px 20px 0 20px;
			box-shadow: 0px 0px 5px 1px rgba(0, 0, 0, .1);
			
			text {
				font-size: 14px;
				font-weight: 700;
			}
			/deep/ uni-button {
				margin: 0;
				flex: 0 0 auto;
				padding: 2px 4px;
				line-height: normal;
			}
		}
		
		.uni-list-box {
			margin-top: -20px;
			padding: 20px 20px 10px 20px;
			
			.list-item {
				padding: 10px;
				margin-top: 10px;
				min-height: 50px;
				background: #fff;
				border-radius: 8px;
				box-shadow: 0px 0px 5px 1px rgba(0, 0, 0, .1);
				
				.list-item-line {
					height: 36px;
					display: flex;
					position: relative;
					align-items: center;
					border-bottom: 1rpx solid #bfbfbf;
					
					.item-text {
						overflow: hidden;
						font-weight: 700;
						white-space: nowrap;
						text-overflow: ellipsis;
					}
					.sub-text {
						color: #999;
						white-space: nowrap;
						
					}
					.short {
						letter-spacing: 32px;
					}
				}
				.title {
					justify-content: space-between;
				}
			}
		}
		
		.popup-search{
			width: 80vw;
			padding-bottom: 10px;
			.search-box{
				padding: 10px 15px;
				.uni-input {
					color: #888;
					line-height: 36px;
				}
			}
		}
	}
	.hide{
		overflow: hidden;
		text-overflow:ellipsis;
		white-space: nowrap;
	}
	.With{
		width: 37%;
	}
</style>
